/*
 *  mini-SaaS
 *  Application for developing Software as a Service (SaaS) 
 *  Copyright (c) 2011 Anand Sudhakaran, Bryce Holton, Chao Zhang
 *  ASU
 *
 *  This file is part of mini-SaaS.
 *
 *  mini-SaaS is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  mini-SaaS is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
 *  along with mini-SaaS; if not, see <http://www.gnu.org/licenses/>. 
 *
 */
 
 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package loginbean;

import java.io.IOException;
import java.sql.*;

import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author vexdan
 */
public class DisplayWebServices extends HttpServlet {


    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        ResultSet rs;
        Statement st = null;
        Connection conn = null;
        ArrayList<loginbean.WebService> wsList= new ArrayList<loginbean.WebService>();
        
        int tenantId = 0;
        int count=0;
                
        try {
        
            conn = new DBManager().getDatabaseConnection();

            HttpSession session = request.getSession(true);
            tenantId = Integer.parseInt(session.getAttribute("tenantid").toString());

            String sql = "select WebServiceID,WebServiceName,WebServiceURL,TenantID from  mydb.webservicetable"
                    + " where TenantID in(?,?)";
            
            PreparedStatement pst = conn.prepareStatement(sql);
            System.out.println(sql);
            pst.setInt(1, tenantId);//
            pst.setInt(2,tenantId);//this has to be our default tenant -0 or 1 for default webservices
            rs = pst.executeQuery();
            
            while(rs.next())
            {
                wsList.add(new WebService(tenantId,rs.getString(3),rs.getString(2),rs.getInt(1),null,null,null));
                //WebService(int tenantID, String url, String name, int id);
                count++;
            }
            pst.close();
            System.out.println("The count value="+count);
            request.setAttribute("webServiceList", wsList);           
            if(count>0)
                response.sendRedirect("displayAvailableWebServices.jsp");
            else
                response.sendRedirect("error.jsp");
                    

           } catch (ClassNotFoundException e) {
            System.out.println("Couldn't load database driver: " + e.getMessage());
        } catch (SQLException e) {
            System.out.println("SQLException caught: " + e.getMessage());
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }    
    }
}